package com.demo.wx;

import com.alibaba.fastjson.JSON;
import com.demo.utils.HttpClientResult;
import com.demo.utils.HttpClientUtil;
import com.demo.wx.base.AccessTokenDto;
import com.demo.wx.base.AccessTokenResultDto;
import com.demo.wx.base.ServiceIpDto;
import com.demo.wx.base.ServiceIpResultDto;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : YU.TAN
 * @date : 2021/10/13 13:59
 */
public class Base {

    private static final String url = "https://gw-api-gateway.gwm.com.cn/crm-service/v1/channel/queryAll";


    private static final String appid = "wxfb3cb94fd76a1667";
    private static final String secret = "4462249da8ebd1b31e94f8b32f4d7519";
    private static final String getAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    private static final String getServerIPURL = "https://api.weixin.qq.com/cgi-bin/get_api_domain_ip";
    private static final String getServerIPCallBackURL = "https://api.weixin.qq.com/cgi-bin/getcallbackip";



    public static void main(String[] args) throws Exception {

        /**
         * 获取access_token
         */
//        AccessTokenDto tokenDto = AccessTokenDto.builder()
//                .appid(appid)
//                .secret(secret)
//                .build();
//        Map<String, String> map = objToMap(tokenDto);
//        HttpClientResult httpClientResult = HttpClientUtil.doGet(getAccessTokenUrl, null, map);
//        String content = httpClientResult.getContent();
//        AccessTokenResultDto tokenResultDto = getResult(content, AccessTokenResultDto.class);

        /**
         * 获取微信服务器IP地址
         */

        ServiceIpDto serviceIpDto = ServiceIpDto.builder()
//                .access_token(tokenResultDto.getAccess_token())
                .access_token("50_vprqHGYyGrKADxWcMjoTjyVinUvIY2bDw2bZaG8HN-oR5Hgk0YFdpCifr9zBGI-QPgs8846FtWMAbhbp56GPBhxq8JXv-nYE_tn6HAyhQ4CHxcCrFJeKCYtiKLr5wY_w47TOybPsm_3eRU1tIILhAHAQQT")
                .build();
        Map<String, String> map = objToMap(serviceIpDto);
        HttpClientResult httpClientResult1 = HttpClientUtil.doGet(getServerIPURL, null, map);
        String content1 = httpClientResult1.getContent();
        ServiceIpResultDto result = getResult(content1, ServiceIpResultDto.class);
        System.out.println(result);

        ServiceIpDto serviceIpDto1 = ServiceIpDto.builder()
//                .access_token(tokenResultDto.getAccess_token())
                .access_token("50_vprqHGYyGrKADxWcMjoTjyVinUvIY2bDw2bZaG8HN-oR5Hgk0YFdpCifr9zBGI-QPgs8846FtWMAbhbp56GPBhxq8JXv-nYE_tn6HAyhQ4CHxcCrFJeKCYtiKLr5wY_w47TOybPsm_3eRU1tIILhAHAQQT")
                .build();
        Map<String, String> map1 = objToMap(serviceIpDto);
        HttpClientResult httpClientResult2 = HttpClientUtil.doGet(getServerIPCallBackURL, null, map1);
        String content2 = httpClientResult2.getContent();
        ServiceIpResultDto result1 = getResult(content2, ServiceIpResultDto.class);
        System.out.println(result1);

    }


    public static <T> T getResult(String result,Class<T> clazz){
        return JSON.parseObject(result, clazz);
    }


    public static Map<String, String> objToMap(Object object) throws Exception {
        Map<String, String> result = new HashMap<>(16);
        Class<?> clazz = object.getClass();
        while (null != clazz.getSuperclass()) {
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field field : declaredFields) {
                String fieldName = field.getName();
                //获取原来的访问控制权限
                boolean accessible = field.isAccessible();
                if (!accessible) {
                    //修改原来的访问控制权限
                    field.setAccessible(true);
                }
                Object value = field.get(object);
                //恢复原来的访问控制权限
                field.setAccessible(accessible);
                if (null != value && StringUtils.isNotBlank(value.toString())) {
                    //如果是List,将List转换为json字符串
                    if (value instanceof List) {
                        value = JSON.toJSONString(value);
                    }
                    result.put(fieldName, value.toString());
                }
            }
            clazz = clazz.getSuperclass();
        }
        return result;
    }
}
